# -- coding: utf-8 --

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt


class photo_deal():

    

    # 灰度处理
    def greyDeal(self):
        img = np.array(Image.open('test.jpg'))
        print('灰度处理')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                grey = (int(bean[0]) + int(bean[1]) + int(bean[2])) / 3
                grey = int(grey)
                bean[0] = bean[1] = bean[2] = grey
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 黑白处理
    def whiteblack(self):
        img = np.array(Image.open('test.jpg'))
        print('黑白处理')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                grey = (int(bean[0]) + int(bean[1]) + int(bean[2])) / 3
                grey = int(grey)
                if grey >= 100:
                    grey = 255
                else:
                    grey = 0
                bean[0] = bean[1] = bean[2] = grey
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 反向滤镜
    def reserve(self):
        img = np.array(Image.open('test.jpg'))
        print('反向滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[0] = 255 - bean[0]
                bean[1] = 255 - bean[1]
                bean[2] = 255 - bean[2]
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 去色滤镜
    def remove(self):
        img = np.array(Image.open('test.jpg'))
        print('去色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                avg = round(
                    (min(int(bean[0]), int(bean[1]), int(bean[2])) + max(int(bean[0]), int(bean[1]), int(bean[2]))) / 2)
                bean[0] = bean[1] = bean[2] = avg
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 红色滤镜
    def red(self):
        img = np.array(Image.open('test.jpg'))
        print('红色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[1] = 0
                bean[2] = 0
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 绿色滤镜
    def green(self):
        img = np.array(Image.open('test.jpg'))
        print('绿色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[0] = 0
                bean[2] = 0
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 蓝色滤镜
    def blue(self):
        img = np.array(Image.open('test.jpg'))
        print('蓝色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[0] = 0
                bean[1] = 0
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 黄色滤镜
    def yellow(self):
        img = np.array(Image.open('test.jpg'))
        print('黄色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[2] = 0
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 紫色滤镜
    def zi(self):
        img = np.array(Image.open('test.jpg'))
        print('紫色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[1] = 0
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 青色滤镜
    def qin(self):
        img = np.array(Image.open('test.jpg'))
        print('青色滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[0] = 0
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 怀旧滤镜
    def huaijiu(self):
        img = np.array(Image.open('test.jpg'))
        print('怀旧滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[0] = int(bean[0])
                bean[1] = int(bean[1])
                bean[2] = int(bean[2])
                newR = 0.393 * bean[0] + 0.769 * bean[1] + 0.189 * bean[2]
                if newR < 0:
                    newR = 0
                if newR > 255:
                    newR = 255
                newG = 0.349 * bean[0] + 0.686 * bean[1] + 0.168 * bean[2]
                if newG < 0:
                    newG = 0
                if newG > 255:
                    newG = 255
                newB = 0.272 * bean[0] + 0.534 * bean[1] + 0.131 * bean[2]
                if newB < 0:
                    newB = 0
                if newB > 255:
                    newB = 255
                bean[0] = newR
                bean[1] = newG
                bean[2] = newB
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 熔铸滤镜
    def rongzhu(self):
        img = np.array(Image.open('test.jpg'))
        print('熔铸滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                bean[0] = int(bean[0])
                bean[1] = int(bean[1])
                bean[2] = int(bean[2])
                newR = (128 * int(bean[0]) / (int(bean[1] + int(bean[2]) + 1)))
                if newR < 0:
                    newR = 0
                if newR > 255:
                    newR = 255
                newG = (128 * int(bean[1]) / (int(bean[0] + int(bean[2]) + 1)))
                if newG < 0:
                    newG = 0
                if newG > 255:
                    newG = 255
                newB = (128 * int(bean[2]) / (int(bean[0] + int(bean[1]) + 1)))
                if newB < 0:
                    newB = 0
                if newB > 255:
                    newB = 255
                bean[0] = newR
                bean[1] = newG
                bean[2] = newB
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.show()

    # 连环画滤镜
    def lianhuan(self):
        img = np.array(Image.open('test.jpg'))
        print('连环画滤镜')
        for i in range(len(img)):
            for j in range(len(img[i])):
                bean = img[i][j]
                r = int(bean[0])
                g = int(bean[1])
                b = int(bean[2])
                newR = abs(g - b + r) * r / 256
                newG = abs(b - g + r) * r / 256
                newB = abs(b - g + r) * bean[1] / 256

                bean[0] = newR
                bean[1] = newG
                bean[2] = newB
                img[i][j] = bean

        plt.figure("after")
        plt.imshow(img)
        plt.axis('off')
        plt.rcParams['font.sans-serif'] = ['SimHei']
        plt.rcParams['axes.unicode_minus'] = False
        plt.title(u'连环画滤镜')
        plt.show()

    def start(self):
        
        self.greyDeal()
        self.whiteblack()
        self.reserve()
        self.remove()
        self.red()
        self.green()
        self.blue()
        self.yellow()
        self.zi()
        self.qin()
        self.huaijiu()
        self.rongzhu()
        self.lianhuan()


if __name__ == '__main__':
    photo_deal().start()
